package com.dailyblue.java.standard.thread.safe;

public class SafeA {

    private int i = 1;

    public int get() {
        return i;
    }

    public synchronized void add() {
        System.out.println(Thread.currentThread().getName()+"执行1了add方法,目前的i:"+i);
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        this.i++;
    }
    private Object obj = new Object();
    public void add2(){
        System.out.println(Thread.currentThread().getName()+"执行1了add方法,目前的i:"+i);
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        synchronized (obj){
            this.i++;
        }
    }
}
